<?php
require_once($application_folder."/controllers/navigator.php");
class Site_users extends navigator 
{

    function __construct()
    {
        parent::__construct();

        $this->load->library('form_validation');
        $this->load->model('users');
    } 

    public function index()
    {
    	$this->load->model('users');
        
    	
    	
        $res['title'] = "User";

        //$this->core_site('user', $res, 'home');
        $this->load->view('user/login', $res);   

    }
    
    function show_login(){
        $this->load->view('user/login');
    }
    
    function show_register(){
        $this->load->view('user/registration');
    }
    
    public function login()
    {
    	//$this->index();
    	
    	$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
    	$this->form_validation->set_rules('passw', 'Lozinku', 'trim|required|md5');
        $this->form_validation->set_message('required', 'Molimo unesite'.' %s.');
        $this->form_validation->set_message('valid_email', 'Molimo unesite validan'.' %s.');
    	
    	if ($this->form_validation->run() == FALSE)
		{
			//Nisu uneti svi podaci idi na login
			//$this->load->view('user/login');
            echo json_encode(array('action' => FALSE, 'msg' => validation_errors()));
		}
		else
		{
			$email = $this->input->post('email');
			$passw = $this->input->post('passw');
			if($this->users->loginUser($email,$passw))
			{
				// korisnik je ulogovan idi na redirekt
				//$this->load->view('user/loged');
                echo json_encode(array('action' => TRUE));
			}
			else
			{
				// Nisu uneti dobri podaci za login vrati na login stranu
				//$this->load->view('user/login');
                echo json_encode(array('action' => FALSE, 'msg' => 'Ne postoji korisnik sa unesenim podacima. Probajte ponovo.'));
			}
		}
    	
    }
    
    public function registration()
    {
    	
    	$this->form_validation->set_rules('firstname', 'Ime', 'required|trim|xss_clean');
    	$this->form_validation->set_rules('lastname', 'Prezime', 'required|trim|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[site_user.email]|callback_email_check');
		$this->form_validation->set_rules('passw', 'Lozinku', 'trim|required|matches[passw1]|md5');
		$this->form_validation->set_rules('passw1', 'Lozinku ponovo', 'trim|required');		
        
        $this->form_validation->set_message('required', 'Molimo unesite'.' %s.');
        $this->form_validation->set_message('valid_email', 'Molimo unesite validan'.' %s.');
        $this->form_validation->set_message('matches', 'Nisu iste lozinke.');
		    	
    	if ($this->form_validation->run() == FALSE)
		{
			//$this->load->view('user/registration');
            echo json_encode(array('action' => false, 'msg' => validation_errors()));
		}
		else
		{
			if($this->users->insertUser($_POST))
			{
				//Posalji mail
				$this->_send_email($_POST['email']);
				//$this->load->view('user/registration_success',$_POST);
                echo json_encode(array('action' => TRUE));
			}
			else
			{
				$data['errors'] = "Nije uspela registracija. Podaci nisu upamceni. Pokusajte ponovo.";
				//$this->load->view('user/registration',$data);
                echo json_encode(array('action' => FALSE, 'msg' => $data['errors']));
			}
		}
    	
    }
    
    /**
     * 
     * Potvrda registracije
     * @param string $email - email adresa korisnika
     */
    public function registration_confirm($email)
    {
    	// E-mail je kriptoivan sa MD5
    	// odobri pristup
    	if( $this->users->userConfirm($email))
    		$this->load->view('user/confirmation_success'); // prikazi da je pristup odobren
    	else 
    		$this->load->view('user/confirmation_failed');  // Pristup nije odobren nepostojeca mail adresa	
    	
    }
    
    public function email_check($email)
    {
    	if($this->users->emailCheck($email))
    	{
    		$this->form_validation->set_message('email_check', 'Email adresa "'.$email.'" vec postoji!');
    		return FALSE;
    	}
    	else
    		return TRUE;
    }
	
    private function _send_email($email)
    {
    	$this->load->library('email');

		$this->email->from('no-replay@dan.com', 'Dnevne novine DAN');
		$this->email->to($email);
		//$this->email->cc('another@another-example.com');
		//$this->email->bcc('them@their-example.com');
		$url = base_url()."site_users/registration_confirm/".md5($email) ;
		$this->email->subject('Potvrda registracija na portalu dnevnih novina DAN');
		$sadrzaj_poruke = "
		
		Da bi ste mogli pristupati portalu dnevnih novina DAN. Morate potvrditi Vaasu regisrtaciju.
		
		Potvrdite Vasu registraciju klikom na sledeci link: <a href='" .$url."'>".$url."</a>;
		
		";
		
		//debug
		//echo $sadrzaj_poruke;
		
		$this->email->message($sadrzaj_poruke);
		
		$this->email->send();
    }
}
